﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Controller;
using EntityObjects;
namespace PresentationLayer.View
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        public string ItemId;
        public string reorderQty;
        RaisePurchaseOrderControl control;
        List<PurchaseOrderDetails> lst;
        static string flag = "false";
        //PurchaseOrder po;


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                control = new RaisePurchaseOrderControl();
                ItemId = Request.QueryString["ItemId"];
                Catalogue c = control.getReorderLvlPurchaseOrderById(ItemId);
                lblCurrentStock.Text = c.TotalQty.ToString();
                lblItemCode.Text = ItemId;
                lblItemName.Text = c.Description;
                lblReorderQty.Text = c.ReOrderQty.ToString();
                foo.DataSource = control.getSupplierTender(ItemId);
                foo.DataBind();
                tot.Value = "First";
                btnOrder.Enabled = false;
                flag = "false";
            }
        }


        protected void qtyText_Changed(object sender, EventArgs e)
        {

            TextBox thisTextBox = (TextBox)sender;
            GridViewRow thisGridViewRow = (GridViewRow)thisTextBox.Parent.Parent;
            int r = thisGridViewRow.RowIndex;

            if (!thisTextBox.Text.Equals("") && !(thisTextBox.Text.Equals("0")))
            {
                int quantity = Convert.ToInt16(thisTextBox.Text);
                int price = Convert.ToInt32(foo.DataKeys[thisGridViewRow.RowIndex].Value);
                Label l = (Label)thisGridViewRow.Cells[3].FindControl("Amount");
                double t = (price * quantity);
                l.Text = t.ToString();

                if (tot.Value == "First") tot.Value = quantity.ToString();
                else tot.Value = (Convert.ToInt16(tot.Value) + Convert.ToInt16(quantity)).ToString();

                btnOrder.Enabled = true;
            }

        }

        protected void btnOrder_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt16(tot.Value.ToString()) >= Convert.ToInt16(lblReorderQty.Text.ToString()))
            {
                foreach (GridViewRow gvr in foo.Rows)
                {
                    string supName = gvr.Cells[0].Text;    //get data from BoundField in each row
                    string qty = ((TextBox)gvr.FindControl("qty")).Text;    //get data from control in TemplateField in each row                      

                    PurchaseOrderDetails tmp = new PurchaseOrderDetails();
                    if (qty != "")
                    {
                        if (Session[supName] == null) lst = new List<PurchaseOrderDetails>();
                        else lst = (List<PurchaseOrderDetails>)Session[supName];
                        tmp.ItemId = lblItemCode.Text;
                        tmp.OrderQty = Convert.ToInt16(qty);
                        tmp.Price = Convert.ToDouble(gvr.Cells[1].Text);
                        if (!lst.Contains(tmp))
                        {
                            lst.Add(tmp);
                            Session[supName] = lst;
                        }
                        err.Text = " ";
                        flag = "true";
                        Session["PO" + tmp.ItemId] = true;
                    }

                }

            }
            else if (Convert.ToInt16(tot.Value.ToString()) <= Convert.ToInt16(lblReorderQty.Text.ToString())) err.Text = "Total Qty Doesn't Match With Reorder Qty";
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Server.Transfer("RaisePurchaseOrder.aspx?Flag=" + flag);
        }           
        }
     
    }
